/* eslint-disable no-new-wrappers */
/* eslint-disable no-prototype-builtins */
/* eslint-disable no-redeclare */
/* eslint-disable no-eq-null */
/* eslint-disable eqeqeq */
/* eslint-disable lets-on-top */
/* eslint-disable no-param-reassign */
/*
 * 常用方法
 * */
let cookiesUtil = {
  getCookie(name) {
    let arr;

    let reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)');

    if ((arr = document.cookie.match(reg))) {
      return arr[2];
    }
    return null;
  },
};

export default {
  host: 'http://172.31.8.10:10000/',
  uploadFile(url, paras) {
    return new Promise(function (resolve) {
      let form = new FormData(); // FormData 对象

      Object.keys(paras).forEach(function (key) {
        form.append(key, paras[key]);
        console.log(key, paras[key]);
      });

      // form.append('uploadFile', rec.getBlob()) // 文件对象
      // let url = '/ncscapi/voices/v1/phoneme/page' // 接收上传文件的后台地址
      let xhr = new XMLHttpRequest(); // XMLHttpRequest 对象

      xhr.open('post', url, false); // post方式，url为服务器请求地址，true 该参数规定请求是否异步处理。
      xhr.setRequestHeader('token', cookiesUtil.getCookie('token'));
      console.log('token', cookiesUtil.getCookie('token'));
      xhr.onload = function (data) {
        resolve(data);
      }; // 请求完成
      // xhr.onerror =  uploadFailed; //请求失败
      xhr.send(form);
      // 开始上传，发送form数据
    });
  },
  getStringLength(val) {
    let str = `${val}`;
    let bytesCount = 0;

    for (let i = 0, n = str.length; i < n; i++) {
      let c = str.charCodeAt(i);

      if ((c >= 0x0001 && c <= 0x007e) || (c >= 0xff60 && c <= 0xff9f)) {
        bytesCount += 1;
      } else {
        bytesCount += 2;
      }
    }
    return bytesCount;
  },
};
